package com.gome.axwl.aoc.web.controller.mq.consumer;

import com.github.pagehelper.PageInfo;
import com.gome.axwl.aoc.common.util.BeanJsonUtil;
import com.gome.axwl.aoc.common.util.CollectionUtil;
import com.gome.axwl.aoc.common.util.DateUtil;
import com.gome.axwl.aoc.entity.driver.DriverWithCarrierDto;
import com.gome.axwl.aoc.entity.dto.*;
import com.gome.axwl.aoc.entity.mq.ImPushMsgMQEntityV31;
import com.gome.axwl.aoc.entity.po.DriverIntegrationPo;
import com.gome.axwl.aoc.entity.po.MpResult;
import com.gome.axwl.aoc.facade.DriverIntegrationFacade;
import com.gome.axwl.aoc.service.DriverIntegrationService;
import com.gome.axwl.aoc.service.GradeRuleService;
import com.gome.axwl.aoc.service.IntegrationRuleService;
import com.gome.axwl.aoc.service.feign.MasterDataFeignClient;
import com.gome.commons.core.ErrorCode;
import com.gome.commons.core.ResultData;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Description: 司机积分mq消费者执行类
 * @Auther: yinyuxin@gome.com.cn
 * @Date: 2018/5/4 10:50
 */
@Component
public class DriverIntegrationConsumerTask extends AbstractConsumer<ImPushMsgMQEntityV31> {

    private static final Logger LOGGER = LoggerFactory.getLogger(DriverIntegrationConsumerTask.class);


    @Autowired
    private DriverIntegrationFacade driverIntegrationFacade;

    @Autowired
    private MasterDataFeignClient masterDataFeignClient;


    @Override
    public ResultData<?> processBusinessLogic(ImPushMsgMQEntityV31 imPushMsgMQEntityV31) {
        LOGGER.info("通过quartz、mq生成司机积分，接收到消息{}", BeanJsonUtil.obj2String(imPushMsgMQEntityV31));
        if (Objects.equals(null,imPushMsgMQEntityV31) ||Objects.equals(null, imPushMsgMQEntityV31.getMpResult())) {
            LOGGER.error("通过quartz、mq生成司机积分，出现异常{}","消息体为null");
            return new ResultData<>(ErrorCode.SUCCESS, ErrorCode.SUCCESS_MSG);
        }
        MpResult mpResult = imPushMsgMQEntityV31.getMpResult();
        String month = mpResult.getMsgContent();
        int exceptionNums=0;
        //从主数据获取所有的司机,因为司机很多，循环执行。
        int pageNum = 1;
        int pageSize = 500;
        Long queryTotal = 0L;
        while (true) {
            ResultData<PageInfo<DriverWithCarrierDto>> driverResult = masterDataFeignClient.queryDriver(null, pageNum, pageSize);
            if (!Objects.equals(ErrorCode.SUCCESS, driverResult.getCode())) {
                LOGGER.error("通过quartz、mq生成司机积分-->调用主数据查询司机信息列表接口(queryDriver)出现异常,参数:pageNum:{},pageSize:{}", pageNum, pageSize);
                exceptionNums++;
            } else if (CollectionUtils.isEmpty(driverResult.getData().getList())) {
                LOGGER.error("通过quartz、mq生成司机积分-->调用主数据查询司机信息列表接口(queryDriver)结果为null,参数:pageNum:{},pageSize:{}", pageNum, pageSize);
               exceptionNums++;
            } else {
                ResultData resultData = driverIntegrationFacade.createDriversIntegration(driverResult.getData().getList(), month);
               if (!Objects.equals(ErrorCode.SUCCESS,resultData.getCode())){
                   LOGGER.error("通过quartz、mq生成司机积分-->新增司机信息列表接口(createDriversIntegration)失败,参数:pageNum:{},pageSize:{}", pageNum, pageSize);
                   exceptionNums++;
               }
            }
            pageNum += 1;
            queryTotal += driverResult.getData().getList().size();
            if (queryTotal >= driverResult.getData().getTotal()) {
                break;
            }
        }
        if (exceptionNums>0){
            return new ResultData<>(ErrorCode.FAILOR,ErrorCode.FAILOR_MSG);
        }
        return new ResultData<>(ErrorCode.SUCCESS,ErrorCode.SUCCESS_MSG);
    }

}
